package com.xrit.controller;

import com.xrit.pojo.Departments;
import com.xrit.pojo.Employess;
import com.xrit.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    //查询
    @RequestMapping("findAll")
    public List findAll(){

        return empService.findAll();
    }

    //添加或删除程序
    @RequestMapping("insert")
    public boolean insert(@RequestBody Employess employess){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        employess.setCreateDate(dateString);
        Integer pd =  empService.insert(employess);
        if(null!= pd){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping("findAllDept")
    public List<Departments> findAllDept(){
        return empService.findAllDept();
    }

    //删除
    @RequestMapping("delete")
    public boolean delete(Integer empId){
        try {
            empService.delete(empId);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    //修改
    @RequestMapping("update")
    public boolean update(@RequestBody Employess employess){
        try {
            empService.update(employess);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
